package ltd.newbee.mall.service.impl;

import com.alibaba.fastjson.JSONArray;
import ltd.newbee.mall.controller.vo.NewBeeMallIndexConfigGoodsVO;
import ltd.newbee.mall.core.CoreMath;
import ltd.newbee.mall.dao.NewBeeMallGoodsMapper;
import ltd.newbee.mall.dao.NewBeeMallOrderItemMapper;
import ltd.newbee.mall.dao.NewBeeMallOrderMapper;
import ltd.newbee.mall.dto.ProductDTO;
import ltd.newbee.mall.dto.RelateDTO;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.NewBeeMallOrder;
import ltd.newbee.mall.entity.NewBeeMallOrderItem;
import ltd.newbee.mall.service.RecommendService;
import ltd.newbee.mall.util.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author root
 * @Description:
 * @Package ltd.newbee.mall.service.impl
 * @date 2021/5/9 10:36
 */
@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private NewBeeMallGoodsMapper newBeeMallGoodsMapper;

    @Autowired
    private NewBeeMallOrderMapper newBeeMallOrderMapper;

    @Autowired
    private NewBeeMallOrderItemMapper newBeeMallOrderItemMapper;


    @Override
    public List<ProductDTO> getProductData(){
        List<ProductDTO> productDTOList = new ArrayList<>();
        List<NewBeeMallGoods> newBeeMallGoodsList = newBeeMallGoodsMapper.selectNewBeeMallGoodsList();
        List<Long> goodIds = newBeeMallGoodsList.stream().map(NewBeeMallGoods::getGoodsId).collect(Collectors.toList());
        for (Long goodId : goodIds) {
            ProductDTO productDTO = new ProductDTO();
            productDTO.setProductId(goodId.intValue());
            productDTOList.add(productDTO);
        }
        return productDTOList;
    }

    @Override
    public List<RelateDTO> getRelateData(){
        List<RelateDTO> relateDTOList = new ArrayList<>();
        List<NewBeeMallOrder> newBeeMallOrders = newBeeMallOrderMapper.selectNewBeeMallOrderList();
        for (NewBeeMallOrder newBeeMallOrder : newBeeMallOrders) {
            List<NewBeeMallOrderItem> newBeeMallOrderItems1 = newBeeMallOrderItemMapper.selectByOrderId(newBeeMallOrder.getOrderId());
            for (NewBeeMallOrderItem newBeeMallOrderItem : newBeeMallOrderItems1) {
                RelateDTO relateDTO = new RelateDTO();
                relateDTO.setUserId(newBeeMallOrder.getUserId().intValue());
                relateDTO.setProductId(newBeeMallOrderItem.getGoodsId().intValue());
//                relateDTO.setIndex((int)(Math.random()*10+1));
                relateDTO.setIndex(1);
                relateDTOList.add(relateDTO);
            }

        }
        return relateDTOList;
    }

    @Override
    public List<NewBeeMallIndexConfigGoodsVO> recommendGoods(String userId){
        List<NewBeeMallIndexConfigGoodsVO> recommendGoods = new ArrayList<>();
        //协同过滤算法
        CoreMath coreMath = new CoreMath();
        //获取商品数据
        List<RelateDTO> relateDTOList = getRelateData();
        //执行算法，返回推荐商品id
        List<Integer> recommendIdLists = coreMath.recommend(Integer.parseInt(userId), relateDTOList);
        //获取商品DTO
        List<ProductDTO> productDTOList = getProductData().stream().filter(e -> recommendIdLists.contains(e.getProductId())).collect(Collectors.toList());
        //获取商品ids
        List<Integer> goodIds = productDTOList.stream().map(e -> e.getProductId()).collect(Collectors.toList());
        List<Long> goodIds2 = JSONArray.parseArray(goodIds.toString(), Long.class);
        //获取所有推荐商品
        List<NewBeeMallGoods> newBeeMallGoods = newBeeMallGoodsMapper.selectByPrimaryKeys(goodIds2);
        //转成VOs
        recommendGoods = BeanUtil.copyList(newBeeMallGoods, NewBeeMallIndexConfigGoodsVO.class);
        //截取商品名字
        for (NewBeeMallIndexConfigGoodsVO newBeeMallIndexConfigGoodsVO : recommendGoods) {
            String goodsName = newBeeMallIndexConfigGoodsVO.getGoodsName();
            String goodsIntro = newBeeMallIndexConfigGoodsVO.getGoodsIntro();
            // 字符串过长导致文字超出的问题
            if (goodsName.length() > 30) {
                goodsName = goodsName.substring(0, 15) + "...";
                newBeeMallIndexConfigGoodsVO.setGoodsName(goodsName);
            }
            if (goodsIntro.length() > 22) {
                goodsIntro = goodsIntro.substring(0, 22) + "...";
                newBeeMallIndexConfigGoodsVO.setGoodsIntro(goodsIntro);
            }
        }
        return recommendGoods;

    }
}
